package itdep.ui.pages.address;

import ga.tapestry.StateConstants;
import ga.tapestry.commonlib.base.pages.EntityPage;
import ga.tapestry.internal.SelectModelBuilder;
import itdep.domain.address.CityType;
import itdep.domain.address.Street;
import itdep.domain.address.StreetType;
import itdep.services.address.StreetService;
import itdep.services.address.StreetTypeService;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.ioc.annotations.Inject;

import java.util.List;

/**
 * @author Ivan Khalopik
 * @version $Revision: 46 $ $Date: 2010-07-04 10:53:08 +0000 (Sun, 04 Jul 2010) $
 */
public class Streets extends EntityPage<Street> {

	@Inject
	private StreetService streetService;

	@Inject
	private StreetTypeService streetTypeService;

	@Inject
	private SelectModelBuilder selectModelBuilder;

	private SelectModel streetTypeModel;

	public SelectModel getStreetTypeModel() {
		if (streetTypeModel == null) {
			final List<StreetType> streetTypes = streetTypeService.getEntities();
			streetTypeModel = selectModelBuilder.buildFormatted(StreetType.class, streetTypes, "%s", "this:description");
		}
		return streetTypeModel;
	}

	@Override
	protected StreetService getEntityService() {
		return streetService;
	}

	public boolean isEditable() {
		final String state = getState();
		return StateConstants.ADD_STATE.equals(state) || StateConstants.EDIT_STATE.equals(state);
	}
}
